using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIHpPrefabItem : UIBaseNode
    {
        private int teamIndex;

        #region Template Generate,don't modify

        protected partial class UIB_UIHpPrefabItem
        {
            #region ObjectBinding Generate

            public IQIGame.Onigao.Framework.ExImage background { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage fill { protected set; get; }
            public GameObject weaknessPoint { protected set; get; }
            public GameObject weaknessBreak { protected set; get; }
            public IQIGame.Onigao.Framework.ExText pointNum { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage[] weaknessTypeList { protected set; get; }

            protected virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("background", out var __tbv0);
                this.background = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("fill", out var __tbv1);
                this.fill = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("weaknessPoint", out var __tbv2);
                this.weaknessPoint = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("weaknessBreak", out var __tbv3);
                this.weaknessBreak = __tbv3;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("pointNum", out var __tbv4);
                this.pointNum = __tbv4;
                var __tbv5 = __binding.GetVariableByName("weaknessTypeList");
                this.weaknessTypeList = new IQIGame.Onigao.Framework.ExImage[__tbv5?.arrayValueCount ?? 0];
                for (int __index = 0; __index < this.weaknessTypeList.Length; __index++)
                {
                    this.weaknessTypeList[__index] = __tbv5.variableArray.GetValue<IQIGame.Onigao.Framework.ExImage>(__index);
                }
            }

            #endregion ObjectBinding Generate
        }

        #endregion Template Generate,don't modify

        public bool active { get; private set; }

        /// <summary>
        /// 可以自定义修改的
        /// </summary>
        protected partial class UIB_UIHpPrefabItem
        {
            public virtual void StartBinding(GameObject __go)
            {
                var binding = __go.GetComponent<ObjectBinding>();
                if (binding != null)
                {
                    this.InitBinding(binding);
                }
            }
        }

        protected UIB_UIHpPrefabItem ui { get; set; }

        protected override void BeforeInit()
        {
            ui = new UIB_UIHpPrefabItem();
            ui.StartBinding(gameObject);
        }

        protected override void OnInit()
        {

        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {
            this.active = false;
        }

        public void SetHp(float rate)
        {
            this.ui.fill.fillAmount = rate;
        }

        public void Initialize(int nTeamIndex, IReadOnlyList<int> rType)
        {
            this.teamIndex = nTeamIndex;
            if (nTeamIndex == BattleConst.TeamRedIndex)
            {
                this.ui.background.sprite = this.resLoader.LoadABAsset<Sprite>(PathConstant.GetBattleSpritePath("HpBar_Player_1"));
                this.ui.fill.sprite = this.resLoader.LoadABAsset<Sprite>(PathConstant.GetBattleSpritePath("HpBar_Player_2"));
                this.ui.weaknessPoint.SetActive(false);
                this.ui.weaknessBreak.SetActive(false);
            }
            else
            {
                this.ui.background.sprite = this.resLoader.LoadABAsset<Sprite>(PathConstant.GetBattleSpritePath("HpBar_Monster_1"));
                this.ui.fill.sprite = this.resLoader.LoadABAsset<Sprite>(PathConstant.GetBattleSpritePath("HpBar_Monster_2"));
            }

            for (int i = 0; i < this.ui.weaknessTypeList.Length; i++)
            {
                var rImg = this.ui.weaknessTypeList[i];
                bool bWeaknessActive = rType?.Count > i;
                rImg.gameObject.SetActive(bWeaknessActive);
                if (bWeaknessActive)
                {
                    rImg.sprite = this.resLoader.LoadABAsset<Sprite>(PathConstant.GetBattleSpritePath("Shield_Monster_" + rType[i]));
                }
            }
            this.Active(true);
        }

        public void SyncPos(UnitView unit, RectTransform parentRect)
        {
            if (!this.active || unit == null)
            {
                return;
            }
            else if (!this.gameObject.activeSelf)
            {
                this.gameObject.SetActive(true);
            }

            var rBindComp = unit.GetComponent<BattleViewBindPointComponent>(ETComponentType.BattleViewBindPointComponent);
            if (rBindComp != null)
            {
                var rBindTrans = rBindComp.GetBindPoint(3);
                Vector3 rPos;
                if (!rBindTrans)
                {
                    var rViewComp = unit.GetComponent<UnitViewComponent>(ETComponentType.UnitViewComponent);
                    rPos = rViewComp.position;
                    rPos.y += 1.5f; // 做个偏移
                }
                else
                {
                    rPos = rBindTrans.position;
                }

                var rLocalPos = ManagerCenter.UI.World2UIPosition(rPos, parentRect);
                this.transform.anchoredPosition = rLocalPos;
            }
        }

        public void SetWeaknessPoint(int nPoint)
        {
            if (this.teamIndex == BattleConst.TeamRedIndex)
            {
                return;
            }
            this.ui.weaknessPoint.SetActive(nPoint > 0);
            this.ui.weaknessBreak.SetActive(nPoint <= 0);

            this.ui.pointNum.text = nPoint.ToString();
        }

        public void Active(bool active)
        {
            if (active != this.active)
            {
                this.active = active;
                // 立即隐藏，展示则在下次刷新位置时展示
                if (this.gameObject && !this.active)
                {
                    this.gameObject.SetActive(this.active);
                }
            }
        }
    }
}